<template>
    <div @keyup.enter="login()">
        <h3>用户登录</h3>
        <el-row>
            <el-col :span="8"></el-col>
            <el-col :span="8">
                <el-form :model="form" label-width="120px">
                    <el-form-item label="手机号">
                        <el-input v-model="telphone"></el-input>
                    </el-form-item>
                    <el-form-item label="密码">
                        <el-input v-model="password" type="password"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="login()">登录</el-button>
                        <el-button type="" @click="register()">注册</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>

<script>
export default {
    data() {
        return {
            telphone: "",
            password: "",
        }
    },
    methods: {
        login() {
            if (this.telphone == "" || this.telphone == null) {
                alert("手机号不能为空");
                return false;
            };
            if (this.password == "" || this.password == null) {
                alert("密码不能为空");
                return false;
            };
            this.$http({
                method: 'POST',
                url: this.$backend+ "/user/login",  
                data: {
                    telphone: this.telphone,
                    password: this.password,
                },
                withCredentials: true,
            })
            .then((result) => {
                if(result.data.status == 'success') {
                    alert("登录成功");
                    this.$router.push('/source/item/list')
                    return true;
                }
                else {
                    alert("登录失败, "+ result.data.data.errMsg)
                }
            }).catch((err) => {
                console.log(err)
                alert("登录超时, 请稍后再试");
                return false;
            });
        },
        register() {
            this.$router.push('/source/user/getotp');
        }
    },
}
</script>
